﻿using System.Linq;
using System.Web.Mvc;
using ComeSeeThis_MVC2.Models;

namespace ComeSeeThis_MVC2.Controllers
{
    public class CompanyController : Controller
    {
        readonly CompanyRepository _companyRepository = new CompanyRepository();

        #region Get Methods

        public ActionResult Index(int page = 0)
        {
            const int pageSize = 15;

            var approved = 1;
            if (User.IsInRole("SiteAdmin")) approved = 3;
            var companies = _companyRepository.GetAllCompanies(approved, 0).OrderBy((c => c.CompanyName));

            var paginatedCompanies = new PaginatedList<Company>(companies, page, pageSize);

            return View(paginatedCompanies);
        }

        public ActionResult Details(int id)
        {
            var company = _companyRepository.GetCompany(id);

            if (company == null) return View("CompanyNotFound");

            return View(company);
        }

        #endregion

        #region Edit Methods

        [Authorize(Roles = "SiteAdmin, ContentAdmin")]
        public ActionResult Edit(int id)
        {
            var company = _companyRepository.GetCompany(id);

            if (company == null) return View("CompanyNotFound");

            return View(company);
        }

        [HttpPost, Authorize(Roles = "SiteAdmin, ContentAdmin")]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            var company = _companyRepository.GetCompany(id);

            if (TryUpdateModel(company))
            {
                _companyRepository.Save();
                return Redirect(Request.QueryString["q"]);
                //return RedirectToAction("Admin");
            }
            return View();
            //return View(company);
        }

        #endregion

        #region Create Methods

        [Authorize]//(Roles = "SiteAdmin, ContentAdmin")]
        public ActionResult Create()
        {
            var company = new Company();

            return View(company);
        }
        [HttpPost, Authorize]//(Roles = "SiteAdmin, ContentAdmin")]
        public ActionResult Create(Company company)
        {
            if (ModelState.IsValid)
            {
                _companyRepository.Add(company);
                _companyRepository.Save();

                if (company.Approved) RedirectToAction("Index");
                return RedirectToAction("Submitted", new { id=company.CompanyId});
            }
            return View();
        }
        public ActionResult Submitted(int id)
        {
            var company = _companyRepository.GetCompany(id);

            return View(company);
        }

        #endregion

        #region Delete Methods

        [Authorize]//(Roles = "SiteAdmin, ContentAdmin")]
        public ActionResult Delete(int id)
        {
            var company = _companyRepository.GetCompany(id);

            return company == null ? View("CompanyNotFound") : View(company);
        }

        [HttpPost, Authorize]//(Roles = "SiteAdmin, ContentAdmin")]
        public ActionResult Delete(int id, string confirmButton)
        {
            var company = _companyRepository.GetCompany(id);
            if (company == null) return View("CompanyNotFound");

            _companyRepository.Delete(company);
            _companyRepository.Save();

            return RedirectToAction("Index");
        }

        #endregion
    }
}
